<template>
  <div class="profile-container" >
    <ts-header title="我的"></ts-header>
    <flex-view >
      <pull-to>
        <ts-group class="ts-profile" >
          <ts-cell :title="name" :text="profileText"
                   theme="profile"
                   :link="checkLink">
            <div slot="avatar">
              <img v-if="image!==null&&image!==''" class="user-avatar" :src="image"/>
              <svg v-else class="user-avatar">
                <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#avatar-default"></use>
              </svg>
            </div>
          </ts-cell>
        </ts-group>
        <ts-group class="ts-menu"  >
          <ts-cell title="认证信息" link="/register/authInfo"></ts-cell>
          <ts-cell title="接单范围" link="/range"></ts-cell>
          <ts-cell title="收益信息" link="/earningsInfo"></ts-cell>
          <ts-cell title="提现管理" link="/manager/cashManager"></ts-cell>
          <ts-cell title="我的评价" link="/rating"></ts-cell>
          <ts-cell title="设置" link="/settings"></ts-cell>
        </ts-group>
      </pull-to>
    </flex-view>
    <ts-tabbar ref="tabbar"></ts-tabbar>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import {GET_PERSONAL_INFO,VERSION} from '../../assets/js/api'

  export default {
    name: 'profile',
    data() {
      return {
        image: null,
        name: ''
      }
    },
    mounted() {
    },
    async created(){
      if ($prod) {
        const string = device.platform;
        const oldVersion = AppVersion.version;
        const json = await this.$http.get(VERSION, null, {show: true, type: 'mini'});
        if (json.result === 1) {
          const newVersion = string === 'Android' ? json.data.store_android_beta : json.data.store_ios_beta;
          const url = string === 'Android' ? json.data.store_android_url : json.data.store_ios_url;
          const content = json.data.market_content;
          if (oldVersion !== newVersion && json.data.store_update_force === 1) {
            this.$modal({message: `为了不影响您的正常使用,请下载最新版本，更新内容: ${content}`, cancel: true,lock: true})
              .then(action => {
                window.location.assign(url)
                this.$toast('正在下载中,请稍后再用');
                navigator.app.exitApp();
              },()=>{
                navigator.app.exitApp();
              })
           }
        }
      }
    },
    async activated(){
      let json = await this.$http.post(GET_PERSONAL_INFO);
      if (json.result === 1) {
        for (let item of Object.keys(json.data)) {
          if (item === 'image'||item==='name') {
            continue
          }
          this.image = json.data.image
          this.name = json.data.name
        }
      }
    },
    computed: {
      profileText() {
        let {token, member_number} = this.currentUser;
        if (member_number && token) {
          let {member_number} = this.currentUser;
          return '编号：' + member_number;
        }
        return '';
      },
      checkLink() {
        return this.currentUser.token ? '/profile/info' : '/profile/info';
      },
      ...mapState([
        'currentUser'
      ])
    }
  }
</script>

<style lang="scss" scoped>
  @import "../../assets/css/mixin";
  @import "../../assets/css/variables";

  .ts-profile {
  }

  .ts-menu {
    margin-top: .2rem;
  }
</style>
